package org.jground.rx.ui.zk.viewControl.extra;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXClass;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXSystem;
import org.jground.rx.RXView;
import org.jground.rx.ui.IUIAttribute;
import org.jground.rx.ui.IUIAttributeView;
import org.jground.rx.ui.zk.UIFrame;
import org.jground.rx.ui.zk.viewControl.UIAttributeFactory;
import org.jground.rx.ui.zk.viewControl.UIAttributeViewForeach;
import org.jground.util.StringUtils;
import org.zkoss.zk.ui.Component;

public class UIAttributeDataPath implements IUIAttribute<Component,UIFrame> {

    public static final String WIDGET_HINT = "extra.datapath";

    public static void register() {
        UIAttributeFactory.register(WIDGET_HINT, new UIAttributeDataPath());
    }

    private static Log logger = LogFactory.getLog(UIAttributeViewForeach.class);

    private String moidRef;
    private String viewRef;
    private String attref;
    private IUIAttribute<Component,UIFrame> uiAttributeRef;
    private IRXObject mod;
    private IRXObject modRef;
    private RXView moViewRef;
    private RXClass moClassRef;

    public UIAttributeDataPath createNew(UIFrame uiFrame, RXView moView) {
        return new UIAttributeDataPath(uiFrame, moView);
    }

    public UIAttributeDataPath() {}

    private UIAttributeDataPath(UIFrame uiFrame, RXView moView) {
        try {

            String path = moView.getProperty("path");
            if (StringUtils.isNotBlank(path)) {
                String[] pathElements = StringUtils.split(path, "/");
                moidRef = pathElements[0];
                viewRef = pathElements[1];
            } else {
                moidRef = moView.getProperty("moidref");
                viewRef = moView.getProperty("viewref");
            }
            attref = moView.getAttref();
            moClassRef = RXSystem.getInstance().getRxService().getRXClass(moidRef);
            moViewRef = moClassRef.getNamedRXView(viewRef);
            uiAttributeRef = UIAttributeFactory.create(uiFrame, moViewRef);
        } catch (Exception e) {
            logger.error(e, e);
        }
    }

    public String getLabel() {
        return uiAttributeRef.getLabel();
    }

    public Component getLabelComponent() {
        return uiAttributeRef.getLabelComponent();
    }

    public Component getControlComponent() {
        return uiAttributeRef.getControlComponent();
    }

    public IUIAttributeView<Component,UIFrame> getUIView() {
        return uiAttributeRef.getUIView();
    }

    public void setData(IRXObject moDataObject) {
        mod = moDataObject;
        String oid = mod.getCurrentValue(attref);
        modRef = RXObjectService.getInstance().getById(oid);
        uiAttributeRef.setData(modRef);
        set(modRef.getValues(viewRef));
    }

    public IRXObject updateData() {
        return mod;
    }

    public void clearData() {}

    public void set(String... values) {
        uiAttributeRef.set(values);
    }

    public String[] get() {
        return uiAttributeRef.get();
    }

    public boolean checkMandatory() {
        return uiAttributeRef.checkMandatory();
    }

    public RXView getRXView() {
        return uiAttributeRef.getRXView();
    }

}
